package com.javaforever.dictionary.serviceimpl;

import com.javaforever.dictionary.dao.DictionaryDao;
import com.javaforever.dictionary.daoimpl.DictionaryDaoImpl;
import com.javaforever.dictionary.database.DBConf;
import com.javaforever.dictionary.domain.Dictionary;
import com.javaforever.dictionary.service.DictionaryService;
import java.sql.Connection;
import java.util.List;

public class DictionaryServiceImpl implements DictionaryService{
	protected DictionaryDao dao = new DictionaryDaoImpl();
	public List<Dictionary> listAllDictionarys() throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.listAllDictionarys(connection);
		}
	}

	public boolean updateDictionary(Dictionary dictionary) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.updateDictionary(connection,dictionary);
		}
	}

	public boolean deleteDictionary(Long dictionaryId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.deleteDictionary(connection,dictionaryId);
		}
	}

	public boolean addDictionary(Dictionary dictionary) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.addDictionary(connection,dictionary);
		}
	}

	public boolean softDeleteDictionary(Long dictionaryId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.softDeleteDictionary(connection,dictionaryId);
		}
	}

	public Dictionary findDictionaryByDictionaryId(Long dictionaryId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.findDictionaryByDictionaryId(connection,dictionaryId);
		}
	}

	public Dictionary findDictionaryByDictionaryName(String dictionaryName) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.findDictionaryByDictionaryName(connection,dictionaryName);
		}
	}

	public List<Dictionary> searchDictionarysByDictionaryName(String dictionaryName) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.searchDictionarysByDictionaryName(connection,dictionaryName);
		}
	}

	public List<Dictionary> listActiveDictionarys() throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.listActiveDictionarys(connection);
		}
	}

	public List<Dictionary> listAllDictionarysByPage(int pagesize,int pagenum) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.listAllDictionarysByPage(connection,pagesize,pagenum);
		}
	}

	public void deleteAllDictionarys(String ids) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			dao.deleteAllDictionarys(connection,ids);
		}
	}

	public void softDeleteAllDictionarys(String ids) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			dao.softDeleteAllDictionarys(connection,ids);
		}
	}

	public Boolean toggleDictionary(Long dictionaryId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.toggleDictionary(connection,dictionaryId);
		}
	}

	public Boolean toggleOneDictionary(Long dictionaryId) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			Dictionary dictionary = dao.findDictionaryByDictionaryId(connection,dictionaryId);
			if (dictionary.getActive()==false) {
				dao.toggleDictionary(connection,dictionaryId);
			}
			else {
				Long count = dao.countActiveDictionaryRecords(connection);
				if (count > 1){
					dao.toggleDictionary(connection,dictionaryId);
				}
			}
			return true;
		}
	}

	public List<Dictionary> searchDictionarysByFieldsByPage(Dictionary dictionary,Long pagenum,Long pagesize) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.searchDictionarysByFieldsByPage(connection,dictionary,pagenum,pagesize);
		}
	}

	public int countDictionarysPage(int pagesize) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.countDictionarysPage(connection,pagesize);
		}
	}

	@Override
	public Long countActiveDictionaryRecords() throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.countActiveDictionaryRecords(connection);
		}
	}

	@Override
	public Long countAllDictionaryRecords() throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.countAllDictionaryRecords(connection);
		}
	}

	@Override
	public Long countSearchDictionarysByFieldsRecords(Dictionary dictionary) throws Exception{
		try (Connection connection = DBConf.initDB()) {
			return dao.countSearchDictionarysByFieldsRecords(connection,dictionary);
		}
	}

}
